using System.Linq;
using Engine;
using Engine.Cameras;
using EngineDrawer.Effects.Bloom;
using EngineDrawer.Sky;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using ThinkDotNetEngine.StaticObjects;
using ThinkDotNetEngineDrawer;
using ThinkDotNetEngineDrawer.Bots;

namespace ThinkDotNetPlatform
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class ThinkDotNetGame : Game
    {
        private readonly string WorldMapPath;
        private BloomEffect _bloomEffect;

        #region Consts

        const string SkyTextureName = @"textures\skydome";
        const string SkyDomeAssetName = @"Objects\skydome";
        
        #endregion
        
        public ThinkDotNetGame(string worldMapPath)
        {
            WorldMapPath = worldMapPath;
            new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";  
        }
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            Services.AddService(typeof (IWorldLoader), new WorldLoader());

            InitCamera();

            _bloomEffect = new BloomEffect(this);

            var arenaDrawer = new ArenaDrawer(this, WorldMapPath);
            Components.Add(arenaDrawer);
            Components.Add(new SkyDomeDrawer(this, SkyDomeAssetName, SkyTextureName));
            Components.Add(new BotsStatusDrawer(this));
            base.Initialize();
            SetHqAsInvisible(arenaDrawer);            
        }

        private void SetHqAsInvisible(ArenaDrawer arenaDrawer)
        {
            foreach (var item in arenaDrawer.World.StaticObjects)
                item.Visible = !(item is BotHq);
        }
        private void InitCamera()
        {
            FppCamera fppCamera = new FppCamera(this);
            const float cameraStartPos = 100f;
            fppCamera.CameraPosition = new Vector3(0, cameraStartPos, 0);
            fppCamera.XAngle = MathHelper.PiOver2;
            Services.AddService(typeof (BaseCamera), fppCamera);
            Components.Add(fppCamera);
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {            
            // Create a new SpriteBatch, which can be used to draw textures.
            new SpriteBatch(GraphicsDevice);

            _bloomEffect.Initialize();            
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            _bloomEffect.BeginDraw();

            base.Draw(gameTime);

            _bloomEffect.Draw(gameTime);
        }
    }
}
